package com.hzjq.core.impl

import com.hzjq.core.BlastDelegate
import com.hzjq.core.bean.ErrorResult
import com.hzjq.core.bean.Param
import com.hzjq.core.callback.Callback
import com.hzjq.core.loader.OnParamLoader
import com.hzjq.core.massage.DataMessageBean
import com.hzjq.core.receive.Receiver
import com.hzjq.core.receive.Receives
import io.reactivex.functions.Consumer

class ParamImpl : OnParamLoader {


    override fun update(param: Param, callback: Consumer<Boolean>) {
        CheckerImpl().setCallback(Consumer {
            if (it) {
                cancel()
                val cmd = BlastDelegate.getDelegate().getAssemblyCmdLoader()
                    .getUpdateParamCmd(param)
                Receives.getInstance().registerReceiver(cmd, object : Receiver {
                    override fun convert(msg: String): Any {
                        return msg
                    }

                    override fun onSuccess(msg: Any) {
                        callback.accept(true)
                    }

                    override fun failed() {
                        callback.accept(false)
                    }

                })

                val msg = DataMessageBean(cmd.cmd)

                BlastDelegate.getDelegate().getCmdExeLoader()
                    .exePollResultCmd(msg.assembly(), object : Callback<Boolean> {
                        override fun onResult(t: Boolean) {
                            callback.accept(true)
                        }

                        override fun onRetryCountChanged(retryCount: Int, action: String) {

                        }

                        override fun onError(errorCode: ErrorResult) {
                            callback.accept(false)
                        }

                    })
            }else {
                callback.accept(false)
            }
        }).start()
    }

    override fun read(callback: Consumer<Param>) {
        CheckerImpl().setCallback(Consumer {
            if (it) {
                val cmd = BlastDelegate.getDelegate().getAssemblyCmdLoader()
                    .getReadParamCmd()
                Receives.getInstance().registerReceiver(cmd, object : Receiver {
                    override fun convert(msg: String): Any {
                        return BlastDelegate.getDelegate().getParseLoader().parseParam(msg)
                    }

                    override fun onSuccess(msg: Any) {
                        if (msg is Param) {
                            callback.accept(msg)
                        }
                    }

                    override fun failed() {
                        val param = Param()
                        param.isSuccess = false
                        callback.accept(param)
                    }

                })

                val msg = DataMessageBean(cmd.cmd)

                BlastDelegate.getDelegate().getCmdExeLoader()
                    .exePollResultCmd(msg.assembly(), object : Callback<Boolean> {
                        override fun onResult(t: Boolean) {

                        }

                        override fun onRetryCountChanged(retryCount: Int, action: String) {

                        }

                        override fun onError(errorCode: ErrorResult) {
                            val param = Param()
                            param.isSuccess = false
                            callback.accept(param)
                        }

                    })
            } else {
                val param = Param()
                param.isSuccess = false
                callback.accept(param)
            }
        }).start()
    }

    override fun cancel() {
        Receives.getInstance().unRegisterReceiver(
            BlastDelegate.getDelegate().getAssemblyCmdLoader()
                .getReadParamCmd()
        )
    }
}